package name.schooley.umarks.domain;

import java.lang.SuppressWarnings;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import name.schooley.umarks.domain.Tag;

privileged aspect Bookmark_Roo_Finder {
    
    @SuppressWarnings("unchecked")
    public static Query Bookmark.findBookmarksByTags(Set<Tag> tags) {
        if (tags == null) throw new IllegalArgumentException("The tags argument is required");
        EntityManager em = Bookmark.entityManager();
        StringBuilder queryBuilder = new StringBuilder("SELECT Bookmark FROM Bookmark AS bookmark WHERE");
        for (int i = 0; i < tags.size(); i++) {
            if (i > 0) queryBuilder.append(" AND");
            queryBuilder.append(" :tags_item").append(i).append(" MEMBER OF bookmark.tags");
        }
        Query q = em.createQuery(queryBuilder.toString());
        int tagsIndex = 0;
        for (Tag _tag: tags) {
            q.setParameter("tags_item" + tagsIndex++, _tag);
        }
        return q;
    }
    
}
